﻿/*
 * Created by SharpDevelop.
 * User: Matjaž
 * Date: 25.12.2008
 * Time: 13:40
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Windows.Forms;

namespace gopher
{
	/// <summary>
	/// Description of FormatPref.
	/// </summary>
	public partial class FormatPref : Form
	{
		string settingsDir = "";
		public FormatPref(string dir)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			settingsDir = dir;
		}
		
		void FormatPrefLoad(object sender, EventArgs e)
		{
			try
			{
				StreamReader SR = new StreamReader(settingsDir+"format.dat");
				while(SR.EndOfStream == false)
				{
					mimeList.Items.Add(SR.ReadLine());
				}
				SR.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		void UpButtonClick(object sender, EventArgs e)
		{
			// Up
            int i = this.mimeList.SelectedIndex;
            object o = this.mimeList.SelectedItem;

            if (i > 0)
            {
                this.mimeList.Items.RemoveAt(i);
                this.mimeList.Items.Insert(i - 1, o);
                this.mimeList.SelectedIndex = i - 1;
            }
		}
		
		void DownButtonClick(object sender, EventArgs e)
		{
			// Down
            int i = this.mimeList.SelectedIndex;
            object o = this.mimeList.SelectedItem;

            if (i < this.mimeList.Items.Count - 1)
            {
                this.mimeList.Items.RemoveAt(i);
                this.mimeList.Items.Insert(i + 1, o);
                this.mimeList.SelectedIndex = i + 1;
            }
		}
		
		void SaveButtonClick(object sender, EventArgs e)
		{
			// Save
			try
			{
				StreamWriter SW = new StreamWriter(settingsDir+"format.dat");
				for(int i = 0; i < mimeList.Items.Count; i++)
				{
					SW.WriteLine(mimeList.Items[i].ToString());
				}
				SW.Flush();
				SW.Close();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		void DeleteButtonClick(object sender, EventArgs e)
		{
			// Delete
			mimeList.Items.Remove(mimeList.Items[mimeList.SelectedIndex]);
		}
		
		void AddButtonClick(object sender, EventArgs e)
		{
			mimeList.Items.Add(mimeBox.Text);
		}
		
		void FormatPrefHelpRequested(object sender, HelpEventArgs hlpevent)
		{
			Help.ShowHelp(this, Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"))+"\\Help\\gopher.chm", "html\\plusformat-dialog.htm");
		}
	}
}
